
global restrict "inbcms==1"

global filename "summarystats.csv"
global munfilename "summarystats_mun.csv"
cap noisily erase "$filename"
cap noisily erase "$munfilename"
global controls childage womanage male dedcat_0 dedcat_1 dedcat_2 dedcat_3 dedcat_4 dedcat_5 wealthscoreorig muslim hhsize hhkidsu5 hhwomen hhsex hhage nlivkids 
global prenatvars anyante  antehosp  
global delivvars delivhome   delivhosp delivother
global vaccvars vaccfull vaccmeas vaccbcg vacchepb vaccdpt vacclastpol   

global muncontrols hh_elec  hh_owntoilet ownland anydevice marrunder20 everborn 
use "birthhist", clear
		keep if year>=2011
		replace inbcms=2 if inbcms==.
		
		sum year
		local min=r(min)
		g monthyear=(year-`min')*12+month
		
		replace muslim=religion==5 if religion<.
			

	**birthvars
	estimates clear		
	preserve
	drop if anyante==.
	bys inbcms: g num=_N 
	eststo: estpost tabstat $prenatvars num, by(inbcms) statistics(mean sd) columns(statistics) 
	esttab est1 using "$filename", main(mean a2) aux(sd a2) nostar unstack nonote label  append

	eststo: estpost ttest $prenatvars, by(inbcms) 
	esttab est2 using "$filename", b(a2)  se(a2) append
	restore
	
	preserve
	drop if delivhome==.
	bys inbcms: g num=_N 
	eststo: estpost tabstat $delivvars  num, by(inbcms) statistics(mean sd) columns(statistics) 
	esttab est3 using "$filename", main(mean a2) aux(sd a2) nostar unstack nonote label  append

	eststo: estpost ttest $delivvars , by(inbcms) 
	esttab est4 using "$filename", b(a2)  se(a2) append
	restore
	
	
	preserve
	drop if vaccany==.
	bys inbcms : g num=_N 	
	eststo: estpost tabstat $vaccvars num, by(inbcms) statistics(mean sd) columns(statistics) 
	esttab est5 using "$filename", main(mean a2) aux(sd a2) nostar unstack nonote label  append

	eststo: estpost ttest $vaccvars, by(inbcms) 
	esttab est6 using "$filename", b(a2) se(a2) append
	restore
	
	keep if $restrict
	keep *_mun *temp month year *prec*  

	
	duplicates drop
	
	**muni vars
	estimates clear
	g num=_N
	eststo: estpost tabstat eventviolent_mun extmaxtemp totprec num, statistics(mean sd) columns(statistics) 
	esttab est1 using "$munfilename", main(mean a2) aux(sd a2) nostar unstack nonote label  

use "curative", clear
	replace inbcms=2 if inbcms==.
		sum year
		local min=r(min)
		g monthyear=(year-`min')*12+month
		

		xi i.edcat, prefix(d) noomit
		
	*Muni controls
	merge m:1 munid using "munistats", keepusing($muncontrols)
		drop if _merge==2
		drop _merge
	
	estimates clear	
	bys inbcms: g num=_N 
	recode hhsex (2=0)
	

	**controls
	estimates clear		
	eststo: estpost tabstat $controls  num , by(inbcms) statistics(mean sd) columns(statistics) 
	esttab est1 using "$filename", main(mean a2) aux(sd a2) nostar unstack nonote label  append

	eststo: estpost ttest $controls , by(inbcms) 
	esttab est2 using "$filename", b(a2) se(a2) append
	
	drop num
	
	**went to facility
	estimates clear	
	preserve
	drop if anysect==.
	bys inbcms: g num=_N 
	eststo: estpost tabstat anysect  num , by(inbcms) statistics(mean sd) columns(statistics) 
	esttab est1 using "$filename", main(mean a2) aux(sd a2) nostar unstack nonote label  append

	eststo: estpost ttest anysect  , by(inbcms) 
	esttab est2 using "$filename", b(a2)  se(a2) append
	restore
	
	keep if $restrict
	keep $muncontrols    munid 
	
	duplicates drop  
	
	*muni controls
	estimates clear
	g num=_N
	eststo: estpost tabstat $muncontrols  num, statistics(mean sd) columns(statistics) 
	esttab est1 using "$munfilename", main(mean a2) aux(sd a2) nostar unstack nonote label  append 
	